package com.windrift.springhibernate.controller;

import com.windrift.common.bean.Option;
import com.windrift.springhibernate.model.AssetType;
import com.windrift.springhibernate.service.AssetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuweigui
 *
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping(value="/assettype")
public class AssetTypeController {

    @Autowired
    private AssetTypeService assetTypeService;


    @RequestMapping(value={"remove/{id}","delete/{id}"}, method=RequestMethod.DELETE)
    @ResponseBody
    public void deleteAssetType(@PathVariable("id") int id) {
        assetTypeService.remove(id);
    }
    @RequestMapping()
    public String list(Model model) {
        List<AssetType> assetTypes = assetTypeService.findAll();
        model.addAttribute("assetTypes", assetTypes);
        return "manage/AssetTypeList";
    }

    @RequestMapping(value={"new","add"}, method = RequestMethod.POST)
    @ResponseBody
    public AssetType createAssetType(@RequestParam("parentId") Integer parentId
            , @RequestParam("name") String name) {
        AssetType assetType = new AssetType();
        assetType.setParentId(parentId);
        assetType.setName(name);
        assetTypeService.add(assetType);
        return assetType;
    }


    //get asset types
    @RequestMapping("/option/{parentId}")
    @ResponseBody
    public List<Option> getAssetTypeOption(@PathVariable("parentId") Integer parentId) {
        List<AssetType> assetTypes  = assetTypeService.findChildren(parentId);
        return toOption(assetTypes);
    }

    @RequestMapping("/option")
    @ResponseBody
    public List<Option> getAssetTypeOption() {
        List<AssetType> assetTypes  = assetTypeService.findAll();
        return toOption(assetTypes);
    }

    private List<Option> toOption(List<AssetType> assetTypes) {
        List<Option> options = new ArrayList<Option>();

        for (AssetType assetType : assetTypes) {
            Option option = new Option();
            option.setId(assetType.getId());
            option.setName(assetType.getName());
            option.setParentId(assetType.getParentId());
            options.add(option);
        }

        return options;
    }


    public void setAssetTypeService(AssetTypeService assetTypeService) {
        this.assetTypeService = assetTypeService;
    }

    
}
